package fortunata.fswapps.omemo;

import com.ecyrd.jspwiki.WikiContext;
import com.hp.hpl.jena.ontology.DatatypeProperty;
import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.vocabulary.RDFS;
import com.hp.hpl.jena.vocabulary.XSD;
import fortunata.FortunataSWApplication;
import org.apache.log4j.Logger;

import java.util.GregorianCalendar;

/**
 * This is the OMEMO SemWebApp.
 */
public class Omemo extends FortunataSWApplication {

    static final public String listWikiPageName = "OmemoStoredOntologies";
    static final public String checksWikiPageName = "OmemoChecksPage";
    static final public String propNameUrl = "url";
    static final public String propNameNS = "ns";
    static final public String propNameAlias = "alias";
    static final public String propNameOntoLang = "ontoLang";
    static final public String propNameVersion = "version";
    static final public String propNameLastCheck = "lastCheck";

    static final public String pluginGenerateListString= "[{fortunata.jspwikiplugins.omemo.GenerateWikiPageContent}]";
    static final public String generatedOntoWikiPagesPrefix = "spec";

    private static Logger log = Logger.getLogger (Omemo.class);

    String alias;
    String altURL;
    String ns;
    String ontLang;
    String versionDate;

    public Omemo(){
         initialize("omemo.model.owl",
                    "omemo.data.owl",
                    "http://nets.ii.uam.es/ontologies/omemo/20060501",
                    "omemo",
                    "ExternalOntology",
                    "OmemoOntology");
    }

    public void addFormData(String alias, String altURL, String ns,
                       String ontLang, String versionDate){
        this.alias = alias;
        this.altURL = altURL;
        this.ns = ns;
        this.ontLang = ontLang;
        this.versionDate = versionDate;
    }
    public String getFormDataAlias(){return alias;}
    public String getFormDataAltURL(){return altURL;}
    public String getFormDataNS(){return ns;}
    public String getFormDataOntLang(){return ontLang;}
    public String getFormDataVersionDate(){return versionDate;}

    /**
     * Mandatory method (forced by FortunataSWApplication). Used by method createIndividual.
     * @return An unique URI por each individual. It uses the information provided by the
     * creation plugin (AddOnto in this case).
     */
    public String getInstanceUniqueURI(){
        return getURI() + alias + versionDate;
    }

    /**
     * Utility for non creational plugins.
     * @param alias
     * @param version
     * @return
     */
    public String getInstanceUniqueURI(String alias, String version){
        return getURI() + alias + version;
    }

    /**
     * Mandatory method (forced by FortunataSWApplication). This method is invoked by writeSemanticData.
     * @param context
     * @param m  The OntModel that is being used. It has been initialized. You must use it just for writing the
     * individual's data. It will be published automatically.
     * @param indl  The individual to be filled with values. It has been created and added to the model. Just fill it.
     */
    public void createIndividual (WikiContext context, OntModel m, Individual indl){
            String URI = getURI();

            Property pUrl = m.getProperty(URI + Omemo.propNameUrl);
            Property pNS = m.getProperty(URI + Omemo.propNameNS);
            Property pAlias = m.getProperty(URI + Omemo.propNameAlias);
            Property pLang = m.getProperty(URI + Omemo.propNameOntoLang);
            Property pVersion = m.getProperty(URI + Omemo.propNameVersion);
            Property pLastCheck = m.getProperty(URI + Omemo.propNameLastCheck);

            indl.setPropertyValue(pUrl, m.createLiteral(altURL));
            indl.setPropertyValue(pNS, m.createLiteral(ns));
            indl.setPropertyValue(pAlias, m.createLiteral(alias));
            indl.setPropertyValue(pLang, m.createLiteral(ontLang));
            indl.setPropertyValue(pVersion, m.createLiteral(versionDate));
            indl.setPropertyValue(pLastCheck, m.createTypedLiteral(new GregorianCalendar()));
    }

    /**
     * Fills up the model with the class (just create it, you will fill it later in method createIndividual),
     * properties and relations. The model will be published automatically.
     * @param m  The model, the implementation details are hidden.
     */
    public void fillDataModel(OntModel m){
        String URI =getURI();
        //Creates a class
        OntClass c1ass = m.createClass(URI + getMainClassName());
        //Creates a datatypeProperty
        DatatypeProperty propURL = m.createDatatypeProperty(URI + Omemo.propNameUrl);
        propURL.setDomain(c1ass);
        propURL.setRange(RDFS.Literal);
        //Creates a datatypeProperty
        DatatypeProperty propNS = m.createDatatypeProperty(URI + Omemo.propNameNS);
        propNS.setDomain(c1ass);
        propNS.setRange(RDFS.Literal);
        //Creates a datatypeProperty
        DatatypeProperty propAlias = m.createDatatypeProperty(URI + Omemo.propNameAlias);
        propAlias.setDomain(c1ass);
        propAlias.setRange(RDFS.Literal);
        //Creates a datatypeProperty
        DatatypeProperty propOntoLang = m.createDatatypeProperty(URI + Omemo.propNameOntoLang);
        propOntoLang.setDomain(c1ass);
        propOntoLang.setRange(RDFS.Literal);
        //Creates a datatypeProperty
        DatatypeProperty propVersion = m.createDatatypeProperty(URI + Omemo.propNameVersion);
        propVersion.setDomain(c1ass);
        propVersion.setRange(RDFS.Literal);
        //Creates a datatypeProperty
        DatatypeProperty propVersionDate = m.createDatatypeProperty(URI + Omemo.propNameLastCheck);
        propVersionDate.setDomain(c1ass);
        propVersionDate.setRange(XSD.dateTime);
    }

}
